<?php
// author - Black. © 2006

class Thumbnail
{
	var $width   = 120;
	var $height  = 80;
	var $quality = 90;
	var $bgcolor = array("r" => 255, "g" => 255 , "b" => 255);
	
	function LoadFile($name)
	{
		$path_parts = pathinfo($name);
		$ext = strtolower($path_parts['extension']);
		if($ext=="gif") return imagecreatefromgif($name);
		if($ext=="png") return imagecreatefrompng($name);
		if($ext=="jpg" || $ext=="jpeg") return imagecreatefromjpeg($name);
		return $im = imagecreatetruecolor( $this->width, $this->height);
	}


	function SaveFile($image, $name, $quality = 0)
	{
		if(file_exists($name)) unlink ($name);
		if($quality==0) $quality=$this->quality;
		
		$path_parts = pathinfo($name);
		$ext = strtolower($path_parts['extension']);
		if($ext=="gif") return ImageGIF($image, $name);
		if($ext=="png") return ImagePNG($image, $name);
		return ImageJPEG($image,$name,$quality);
	}


	function Generate($name, $newname, $x = 0, $y = 0, $oneside = false)
	{
		$load_image=$this->LoadFile($name);
		$ix = imagesx($load_image);
		$iy = imagesy($load_image);
		
		if($GLOBALS['not_enlarge_thumbs'] && $ix<=$x && $iy<=$y)
		{
			return $this->SaveFile($load_image, $newname);
		}
		
		if(!$oneside)
		{
			if($x==0) $x=$this->width;
			if($y==0) $y=$this->height;
			if($ix>=$x && $iy>=$y) 
//			both dimensions are greater than requested - we reduce
				$final_image = &$this->ReduceStrategy( $load_image, $x, $y, $ix, $iy);
			else
//			at least one dimension are smaller than requested - we crop
				$final_image = &$this->CropStrategy( $load_image, $x, $y, $ix, $iy);
		}
		else
		{
			if($iy/$y>$ix/$x)
			{
				$x = floor($ix*($y/$iy));
				$final_image = &$this->ReduceStrategyOneSide( $load_image, $x, $y, $ix, $iy);
			}
			else
			{
				$y = floor($iy*($x/$ix));
				$final_image = &$this->ReduceStrategyOneSide( $load_image, $x, $y, $ix, $iy);
			}
		}
		
		return $this->SaveFile($final_image, $newname);
	}
	
	
	function &ReduceStrategy(&$image, $x, $y, $ix, $iy)
	{
		$tx=$x;
		$ty=$y;
		while(1){
			if(1.25*$tx<$ix && 1.25*$ty<$iy){
				$tx=1.25*$tx;
				$ty=1.25*$ty;
			}
			else break;
		}
		$temp_image = imagecreatetruecolor($tx,$ty);
		$dx=($ix-$tx)/2;
		$dy=($iy-$ty)/2;
		ImageCopy($temp_image, $image, 0,0, $dx,$dy, $tx, $ty);
		
		$final_image = imagecreatetruecolor($x,$y);
		ImageCopyResampled($final_image, $temp_image, 0,0,0,0, $x,$y, $tx, $ty);
		
		return $final_image;
	}
	
	function &ReduceStrategyOneSide(&$image, $x, $y, $ix, $iy)
	{
		$final_image = imagecreatetruecolor($x,$y);
		ImageCopyResampled($final_image, $image, 0,0,0,0, $x,$y, $ix, $iy);
		
		return $final_image;
	}
	
	function &CropStrategy(&$image, $x, $y, $ix, $iy)
	{
		$cx=$x/2;
		$cy=$y/2;
		$cix=$ix/2;
		$ciy=$iy/2;
		
		$nx = $cx - $cix;
		$ny = $cy - $ciy;
		
		$final_image = imagecreatetruecolor($x,$y);
		$backgroung_color = imagecolorallocate($final_image, $this->bgcolor["r"], $this->bgcolor["g"], $this->bgcolor["b"]); 
		imagefilledrectangle ( $final_image , 0, 0, $x, $y, $backgroung_color);
		
		ImageCopy($final_image, $image, $nx, $ny, 0,0, $ix, $iy);
		return $final_image;
	}
	
	
}



?>